'use strict';

//Directive used to set metisMenu and minimalize button
angular.module('inspinia')
    .directive('sideNavigation', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element) {
                // Call metsi to build when user signup
                scope.$watch('authentication.user', function() {
                    $timeout(function() {
                        element.metisMenu();
                    });
                });

            }
        };
    })
    .directive('minimalizaSidebar', function ($timeout) {
        return {
            restrict: 'A',
            template: '<a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="" ng-click="minimalize()"><i class="fa fa-bars"></i></a>',
            controller: function ($scope,$rootScope, $element) {
                $scope.minimalize = function () {
                    angular.element('body').toggleClass('mini-navbar');
                    if (!angular.element('body').hasClass('mini-navbar') || angular.element('body').hasClass('body-small')) {
                        // Hide menu in order to smoothly turn on when maximize menu
                        angular.element('#side-menu').hide();
                      $rootScope.sideBarHidden=false;
                        // For smoothly turn on menu
                        $timeout(function () {
                            angular.element('#side-menu').fadeIn(400);
                        }, 200);
                    }else if ($('body').hasClass('fixed-sidebar')) {
                      //
                      $rootScope.sideBarHidden=false;
                      $('#side-menu').hide();
                      setTimeout(
                        function () {
                          $('#side-menu').fadeIn(400);
                        }, 100);
                    } else {
                        // Remove all inline style from jquery fadeIn function to reset menu state
                      $rootScope.sideBarHidden=true;
                      angular.element('#side-menu').removeAttr('style');
                    }
                };
            }
        };
    });

/**
 * iboxTools - Directive for iBox tools elements in right corner of ibox
 */
function iboxTools($timeout) {
  return {
    restrict: 'A',
    scope: true,
    templateUrl: 'components/common/ibox_tools.html',
    controller: function ($scope, $element) {
      // Function for collapse ibox
      $scope.showhide = function () {
        var ibox = $element.closest('div.ibox');
        var icon = $element.find('i:first');
        var content = ibox.find('div.ibox-content');
        content.slideToggle(200);
        // Toggle icon from up to down
        icon.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down');
        ibox.toggleClass('').toggleClass('border-bottom');
        $timeout(function () {
          ibox.resize();
          ibox.find('[id^=map-]').resize();
        }, 50);
      };
      // Function for close ibox
      $scope.closebox = function () {
        var ibox = $element.closest('div.ibox');
        ibox.remove();
      }
    }
  };
}

/**
 * iboxTools with full screen - Directive for iBox tools elements in right corner of ibox with full screen option
 */
function iboxToolsFullScreen($timeout) {
  return {
    restrict: 'A',
    scope: true,
    templateUrl: 'components/common/mibox_tools_full_screen.html',
    controller: function ($scope, $element) {
      // Function for collapse ibox
      $scope.showhide = function () {
        var ibox = $element.closest('div.ibox');
        var icon = $element.find('i:first');
        var content = ibox.find('div.ibox-content');
        content.slideToggle(200);
        // Toggle icon from up to down
        icon.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down');
        ibox.toggleClass('').toggleClass('border-bottom');
        $timeout(function () {
          ibox.resize();
          ibox.find('[id^=map-]').resize();
        }, 50);
      };
      // Function for close ibox
      $scope.closebox = function () {
        var ibox = $element.closest('div.ibox');
        ibox.remove();
      };
      // Function for full screen
      $scope.fullscreen = function () {
        var ibox = $element.closest('div.ibox');
        var button = $element.find('i.fa-expand');
        $('body').toggleClass('fullscreen-ibox-mode');
        button.toggleClass('fa-expand').toggleClass('fa-compress');
        ibox.toggleClass('fullscreen');
        // ibox.toggleClass('lm-ibox-fullscreen');
        setTimeout(function() {
          $(window).trigger('resize');
        }, 100);
      }
    }
  };
}


/*
姓名输入
 */
function nameInput() {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/common/nameInput.html',
    controller: function ($scope) {
      $scope.minlength=$scope.minlength||2;
      $scope.maxlength=$scope.maxlength||20;
    }
  }
}
/*
本地图片预览
 */
function fileModel($parse) {
  return {
    restrict: 'A',
    link: function (scope, element, attrs, ngModel) {
      var model = $parse(attrs.fileModel);
      var modelSetter = model.assign;
      element.bind('change', function (event) {
        scope.$apply(function () {
          modelSetter(scope, element[0].files[0]);
        });
        //附件预览
        scope.file = (event.srcElement || event.target).files[0];
        scope.getFile();
      });
    }
  };

}


function errSrc() {
  return {
    link: function (scope, element, attrs) {
      element.bind('error', function () {
        if (attrs.src != attrs.errSrc) {
          attrs.$set('src', attrs.errSrc);
        }
      });
    }
  }
}


function fileModel2($parse) {
  return {
    restrict: 'A',
    link: function (scope, element, attrs, ngModel) {
      var model2 = $parse(attrs.fileModel2);
      var modelSetter2 = model2.assign;
      element.bind('change', function (event) {
        scope.$apply(function () {
          modelSetter2(scope, element[0].files[0]);
        });
        //附件预览
        scope.file = (event.srcElement || event.target).files[0];
        scope.getFile2();
      });
    }
  };

}
function fileread() {
  return {
    scope: {
      fileread: "="
    },
    link: function (scope, element, attributes) {
      element.bind("change", function (changeEvent) {
        var reader = new FileReader();
        reader.onload = function (loadEvent) {
          scope.$apply(function () {
            scope.fileread = loadEvent.target.result;
          });
        }
        reader.readAsDataURL(changeEvent.target.files[0]);
      });
    }
  }
}
function myInput() {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/directiveTmp/myInput.html',
    controller: function ($scope) {
      $scope.$watch('searchTerm', function (newValue, oldValue) {
        if (newValue != oldValue) {
          $scope.handleTerm = $scope.searchTerm.toLowerCase();
          $scope.handleTerm = $scope.handleTerm.replace(/\s+/g, '');
        }
      })
    }
  }
}
//
function myInput1() {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/directiveTmp/myInput1.html',
    controller: function ($scope) {
      $scope.$watch('searchTerm1', function (newValue, oldValue) {
        if (newValue != oldValue) {
          $scope.handleTerm1 = $scope.searchTerm1.toLowerCase();
          $scope.handleTerm1 = $scope.handleTerm1.replace(/\s+/g, '');
        }
      })
    }
  }
}
//
function myTimepicker(handleTime) {
  return {
    restrict: 'E',
    scope:false,
    templateUrl: 'components/directiveTmp/myTimepicker.html',
    controller: function ($scope) {
      $scope.visible1 = true;
      $scope.visible2 = true;
      $scope.switchvisible1 = function () {
        $scope.visible1 = !$scope.visible1;
        $scope.searchStartTime = $scope.visible1 == false ? handleTime($("#studystartdate").val()) : -1;
      }
      $scope.switchvisible2 = function () {
        $scope.visible2 = !$scope.visible2;
        $scope.searchEndTime = $scope.visible2 == false ? handleTime($("#studyenddate").val()) : 99999999;
      }
      $scope.changeDate1=function () {
        $scope.searchStartTime = $scope.visible1 == false ? handleTime($("#studystartdate").val()) : -1;
      }
      $scope.changeDate2=function () {
        $scope.searchEndTime = $scope.visible2 == false ? handleTime($("#studyenddate").val()) : -1;
      }
      $scope.searchEndTime = $scope.visible2 == false ? handleTime($("#studyenddate").val()) : 99999999;
      $scope.searchStartTime = $scope.visible1 == false ? handleTime($("#studystartdate").val()) : -1;

    }

  };
}
//
function myTimepicker1(handleTime) {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/directiveTmp/myTimepicker1.html',
    controller: function ($scope) {
      $scope.visible3 = true;
      $scope.visible4 = true;
      $scope.switchvisible3 = function () {
        $scope.visible3 = !$scope.visible3;
        $scope.searchStartTime1 = $scope.visible3 == false ? handleTime($("#studystartdate1").val()) : -1;
      }
      $scope.switchvisible4 = function () {
        $scope.visible4 = !$scope.visible4;
        $scope.searchEndTime1 = $scope.visible4 == false ? handleTime($("#studyenddate1").val()) : 99999999;
      }
      $scope.changeDate3=function () {
        $scope.searchStartTime1 = $scope.visible3 == false ? handleTime($("#studystartdate1").val()) : -1;
      }
      $scope.changeDate4=function () {
        $scope.searchEndTime1 = $scope.visible4 == false ? handleTime($("#studyenddate1").val()) : -1;
      }
      $scope.searchEndTime1 = $scope.visible4 == false ? handleTime($("#studyenddate1").val()) : 99999999;
      $scope.searchStartTime1 = $scope.visible3 == false ? handleTime($("#studystartdate1").val()) : -1;
    }
  };
}
/*
footable加载完成过后，进行刷新
 */
function myFootable() {
  //wenjinag.ji at 2016/07/09
  //reference:http://stackoverflow.com/questions/20243339/footable-along-with-angular-js
  return function (scope, element) {
    var footableTable = element.parents('table');
    if (!scope.$last) {
      return false;
    }
    scope.$evalAsync(function () {
      if (!footableTable.hasClass('footable-loaded')) {
        footableTable.footable();
      }
      footableTable.trigger('footable_initialized');
      footableTable.trigger('footable_resize');
      footableTable.data('footable').redraw();
    });
  };
  //wenjinag.ji at 2016/07/09,end
}
/**
 *
 * Pass all functions into module
 */
angular
  .module('inspinia')
  .directive('iboxTools', iboxTools)
  .directive('iboxToolsFullScreen', iboxToolsFullScreen)
  .directive('nameInput',nameInput)
  .directive('errSrc', errSrc)
  .directive('fileModel',fileModel)
  .directive('fileModel2',fileModel2)
  .directive('fileread',fileread)
  .directive('myFootable', myFootable)
  .directive('myInput', myInput)
  .directive('myInput1', myInput1)
  .directive('myTimepicker', myTimepicker)
  .directive('myTimepicker1', myTimepicker1)



